<template>
    <div class='approval'>
      <el-card class="box-card">
        <div class="head-title2">
          <div class='title'>
            {{$t('achievements.title1')}}
          </div>
          <div class="info">
            <el-tooltip placement="left">
              <div slot="content">
                {{define}}
              </div>
              <div class="btns"></div>
            </el-tooltip>
          </div>
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
              <!-- 审核时间 -->
              <div class='searchItem searchItem-date'>
                <span>{{$t('EndApprReport.apprTime')}}</span>
                <el-date-picker
                size='small'
                v-model="listQuery.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="$t('rule.beginDate')">
                </el-date-picker>
                <b>{{$t('approval.go')}}</b>
                <el-date-picker
                size='small'
                  v-model="listQuery.endDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.endDate')">
                </el-date-picker>
              </div>
              <!-- 审核员 -->
              <div class='searchItem'>
                  <span>{{$t('EndApprReport.loanUser')}}</span>
                  <el-input v-model='listQuery.approvalUser' :placeholder="$t('rule.loanUser')" size='small'></el-input>
              </div>
              <!-- 选择组 -->
              <div class='searchItem'>
                <span>{{$t('EndApprReport.chocieGroup')}}</span>
                <el-select v-model='listQuery.groupIds' size='small' multiple>
                  <el-option
                  v-for='item in collGroup'
                  :key='item.groupId'
                  :label='item.groupName'
                  :value='item.groupId'>
                  </el-option>
                </el-select>
              </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
                <button type='button' @click="handleDownload" :disabled="expDis" v-if="apprUser_btn_export">
                    {{$t('btn.export')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('achievements.title2')}}
          </div>
          <el-table
              :sum-text="$t('ApprReport.total')"
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              style='width: 100%'
              @selection-change='handleSelectionChange'
              border
              size='mini'>
              <!-- <el-table-column
                :label="$t('user.num')"
                header-align='center'
                align='center'
                width="60">
                <template slot-scope="scope">
                  <span>{{scope.row.index}}</span>
                </template>
              </el-table-column> -->
              <!-- 时间 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='crtTime'
              :label="$t('approvalDetail.riskTime')">
              </el-table-column>
              <!-- 组名称 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='finalCheckGroup'
              :label="$t('EndApprReport.firstGroup')">
              </el-table-column>
              <!-- 审核员 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='finalCheckUser'
              :label="$t('EndApprReport.loanUser')">
              </el-table-column>
              <!-- 审核数量 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='approvalCount'
              :label="$t('EndApprReport.groupHandle')">
              </el-table-column>
              <!-- 通过数量 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='passCount'
              :label="$t('EndApprReport.groupAdopt')">
              </el-table-column>
              <!-- 通过金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='passMoney'
              :formatter="cashFormatter"
              :label="$t('EndApprReport.passMoney')">
              </el-table-column>
              <!-- 通过率 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='passRate'
              :label="$t('EndApprReport.groupRate')">
              </el-table-column>
              <!-- 订单首逾率（%） -->
              <el-table-column
              header-align='center'
              align='center'
              prop='firstOverdueRate'
              :label="$t('EndApprReport.firstOver')">
              </el-table-column>
              <!-- 金额首逾率（%） -->
              <el-table-column
              header-align='center'
              align='center'
              prop='firstMoneyRate'
              :label="$t('EndApprReport.moneyOverdueRate')">
              </el-table-column>
              
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[5, 10, 20, 30]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { padLeftZero, formatCash } from 'utils/index'
import { getGroupListByType, pageGroup, epxortExsl } from 'api/reportForm/EndApprReport/index'
export default {
  data() {
    return {
      define: '',
      apprUser_btn_export: false,
      approvalCount: "",
      passCount: "",
      passRate: "",
      firstOverdueRate: "",
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 5,
        page: 1,
        // groupIds: '',
        // 审核员
        approvalUser: '',
        // 审核组
        groupIds: '',
        // 开始时间
        startDate: '',
        // 结束时间
        endDate: '',
        // 类型   1 终审报表(人)  2终审报表(组)  3终审报表(总)
        type: 1,
        // 1自动审批 2 人工审批
        approvalType: ''
      },
      tableData: [],
      multipleSelection: [],
      collGroup: [
        {
          groupId: "",
          groupName: this.$t("rule.all")
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", 'getReportDefine'])
  },
  created() {
    // if (this.apprUserPage.has(this.$route.path)) {
    //   this.listQuery = this.apprUserPage.get(this.$route.path);
    // }
    this.getTime();
    this.getList();
    // 催收小组
    getGroupListByType({
      code: 'finalApprove'
    })
    .then(response => {
      this.collGroup = this.collGroup.concat(response.data)
      // console.log(this.collGroup)
    })
    this.apprUser_btn_export = this.elements["apprUser:btn_export"];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'FINAL_REVIEW_PEOPLE') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    getTime() {
      this.listQuery.startDate = this.GetDateStr(-7);
      this.listQuery.endDate = this.GetDateStr(0);
      // this.listQuery.observeDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = padLeftZero(dd.getMonth() + 1, 2);
      const d = padLeftZero(dd.getDate(), 2);
      return y + '-' + m + '-' + d;
    },
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue)
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 0:
            sums[index] = this.$t("ApprReport.total");
            break;
          case 4:
            sums[index] = this.approvalCount;
            break;
          case 5:
            sums[index] = this.passCount;
            break;
          case 6:
            sums[index] = this.passRate;
            break;
          case 7:
            sums[index] = this.firstOverdueRate;
            break;
          default:
            sums[index] = "--";
            break;
        }
      });
      return sums;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      epxortExsl(this.listQuery).then(response => {
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/vendor/Export2Excel");
          const tHeader = [
            this.$t("approvalDetail.riskTime"),
            this.$t("EndApprReport.firstGroup"),
            this.$t("EndApprReport.loanUser"),
            this.$t("EndApprReport.groupHandle"),
            this.$t("EndApprReport.groupAdopt"),
            this.$t('EndApprReport.passMoney'),
            this.$t("EndApprReport.groupRate"),
            this.$t("EndApprReport.firstOver"),
            this.$t('EndApprReport.moneyOverdueRate')
          ];
          const filterVal = [
            "crtTime",
            "finalCheckGroup",
            "finalCheckUser",
            "approvalCount",
            "passCount",
            "passMoney",
            "passRate",
            "firstOverdueRate",
            "firstMoneyRate"
          ];
          const list = response.data;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, this.$t("EndApprReport.apprUser"));
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    getList() {
      this.listLoading = true;
      // this.$store.dispatch("saveApprUserPage", {
      //   path: this.$route.path,
      //   listQuery: this.listQuery
      // });
      pageGroup(this.listQuery).then(response => {
        response.data.rows.forEach((item, index) => {
          item.index =
            index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
        });
        this.expDis = false;
        this.listLoading = false;
        this.tableData = response.data.rows;
        this.total = Number(response.data.total);
        // this.approvalCount = response.data.approvalCount;
        // this.passCount = response.data.passCount;
        // this.passRate = response.data.passRate;
        // this.firstOverdueRate = response.data.firstOverdueRate;
      });
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.getTime();
      // this.listQuery.startDate = "";
      // this.listQuery.endDate = "";
      this.listQuery.groupIds = [];
      this.listQuery.approvalUser = "";
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>